package assignments.sagarjoshi.springtms;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class LoginScreen extends Activity {

    EditText eUsername, ePassword;
    String sUsername, sPassword;
    Button bLogin, bReset;
    String role;
    Activity context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_screen);
        context=this;
        ActionBar b = getActionBar();
        b.setTitle("Login");
        b.show();

        /*This contains all initialization part*/
        eUsername = (EditText) findViewById(R.id.editText1);
        ePassword = (EditText) findViewById(R.id.editText2);
        bLogin = (Button) findViewById(R.id.button);
        bReset = (Button) findViewById(R.id.button2);



        bLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                sUsername = eUsername.getText().toString();
                sPassword = ePassword.getText().toString();

                if (sUsername.isEmpty()){
                    Toast.makeText(getApplicationContext(),"Username cannot be null...",Toast.LENGTH_LONG).show();
                    eUsername.requestFocus();
                }
                if (sPassword.isEmpty()){
                    Toast.makeText(getApplicationContext(),"Password cannot be null...",Toast.LENGTH_LONG).show();
                    ePassword.requestFocus();
                }
                else{
                  MyDatabaseHelper mdh = new MyDatabaseHelper(getApplicationContext());
                    SQLiteDatabase sqLiteDatabase = mdh.getReadableDatabase();
                    Login login = new Login(sUsername,sPassword);
                    Cursor cursor=mdh.CheckLogin(getApplicationContext(),login,sUsername,sPassword);
                    if (cursor==null){
                        Toast.makeText(getApplicationContext(), "Could not fetch data...", Toast.LENGTH_LONG).show();

                    } else if( cursor.getCount()==0){
                        Toast.makeText(getApplicationContext(), "Incorrect username/password...", Toast.LENGTH_LONG).show();
                    }
                   else {
                        Toast.makeText(getApplicationContext(), "Login successful...", Toast.LENGTH_LONG).show();
                        cursor.moveToFirst();
                        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("session", getApplicationContext().MODE_PRIVATE);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("email_id", sUsername);
                        role = cursor.getString(2);
                        editor.putString("role", role);
                        editor.commit();


                        if (role.equals("admin")) {
                            Intent intent = new Intent(LoginScreen.this, Activity_AdminHome.class);
                            startActivity(intent);
                            context.finish();
                        }
                        if(role.equals("trainee")){
                            Intent intent = new Intent(LoginScreen.this, Activity_TraineeHome.class);
                            //Following is actual screen

                            startActivity(intent);
                            context.finish();
                        }
                    }
                }


            }
        });

        bReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                eUsername.setText("");
                ePassword.setText("");
            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        return super.onOptionsItemSelected(item);
    }
}
